package com.athbsd.Map;

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;

/**
 * Date:2025/2/24
 * Author：zhouzhou
 * Description:
 */
public class CityMapTest {
    public static void main(String[] args) {
        Map map = CityMap.model;
        Set provice=map.keySet();
        for (Object o:provice) {
            System.out.print(o+"\t\t");
        }

        Scanner sc = new Scanner(System.in);
        String[] cities;
        while(true){
            System.out.print("\n请选择你所在的省份:");
            String inputProvice = sc.nextLine();
            //map.get(key)-->可以获得对应value;
            //如果填对,返回对应value,如果填错,返回null或者.length=0
            cities=(String[])map.get(inputProvice);
            if(cities==null||cities.length==0){
                System.out.println("你输入的省份有误,请重新输入:");
            }else {
                //输入如果存在,跳出循环
                break;
            }
        }
        for (String city:cities){
            System.out.print(city+"\t\t");
        }
       l: while (true){
            System.out.println("请选择你所在的城市");
            String inputcity = sc.next();
            //方式1:
//            for (int i=0;i<cities.length;i++){
//                if (cities[i].equals(inputcity)){
//                    System.out.println("信息登记成功");
//                    break l;
//                }
//            }
            if(contain(cities,inputcity)){
                System.out.println("信息登记成功");
                break;
            }
           System.out.println("你输入的城市有误.");
        }

        sc.close();
    }

    //方式2:
    public  static boolean contain(String[] cities,String city){
        for (int i = 0; i < cities.length; i++) {
            if (cities[i].equals(city)){
                return true;
            }
        }
        return false;
    }
}


class CityMap{
    public static Map model=new HashMap();
    static{
        model.put("辽宁",new String[]{"沈阳","盘锦","丹东"});
        model.put("河南",new String[]{"郑州","洛阳","开封"});
        model.put("河北",new String[]{"天津","唐山","邯郸"});
    }
}
